#include "byhh.h"

using namespace byhh_variables;

QString byhhArticle::reply(){
    QStringList sentences = content.split("\n");
    QString value = trUtf8("\n【 在 %1 (%2) 的大作中提到: 】")
		    .arg(author->getProperty("id"))
		    .arg(author->getProperty("nick"));
    foreach(QString s, sentences)
	if (s.length() > 0) value += tr("\n:%1").arg(s);
    return value;
}

QString byhhArticle::display(){
    QFile file(":/byhh/article.html");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
	return QString(tr("error while open byhh/article.html"));
    QString html = file.readAll();
    html = html.replace("%content%", string2Html(content));
    html = html.replace("%signature%", string2Html(signature));
    qDebug() << html;
    return html;
}

QString byhhArticle::string2Html(QString str) const{
    QRegExp colorA("\e\[m");
    QRegExp colorB("\e\[[10;]*(\\d*)m");
    QRegExp colorC("\e\[(\\d*)[;10]*m");
    QString color("</font><font class=\"c\\1\" id=\"a\">");
//     str.replace(QRegExp("\n"), "<br/>\n");
    str.replace(colorA, "");
    str.replace(colorB, color);
    str.replace(colorC, color);
    QString html = QString("<p><pre>%1</pre></p>").arg(str);
    return html;
    /*
    QString smiles[]={	"anger", "bad", "cool", "cry", "dog",
			"envy", "fear", "grin", "kill", "love",
			"pig", "puke", "question", "shock","shuai",
			"shy", "sleepy", "smile", "smoke", "stupid",
			"sweat", "thumbdown", "unhappy", "uplook", "zan"};
    for (int i = 0; i < sizeof (smiles); i ++){
	
    }
    */
}

void byhhTopic::refreshMe(){
    //DEBUG_F();
    ///This functin is to get a newer Article list
    emit newStatus(221, tr("Check topic '%1'...").arg(name));
    QHttp * http = new QHttp(server.host() , server.port(80));
    QObject::connect (http , SIGNAL(requestFinished(int , bool)) , this , SLOT(extractArticle(int , bool)));
    QByteArray query = queryViewTopic.arg(board).arg(id).toUtf8();
    int i = request(http, profile, query);
    httpMaps.insert(i , http);
}


/*
We are about to split data from like follow:

_M.1237115464.A" class="hide">
发信人: Tezesut (passion85), 信区: PopMusic
标  题: 请问：《对不起我爱你》里那小提琴插曲叫什么？
发信站: 武汉白云黄鹤站 (2009年03月15日19:11:04 星期天)


rt
--
http://bbs.whnet.edu.cn/f/KAOYAN/1236066281/em060120015.jpg
[1;33m※ 来源:·武汉白云黄鹤站 bbs.whnet.edu.cn·[m
</textarea>

*/
void byhhTopic::extractArticle(int num , bool error){
    ///To get article list from a html
    QString html = fetchHtml(num , error);
    if (html == "") return;
    DEBUG_F();
    profile-> updateCookie(html);
    
    int pos = 0;
    byhhArticle * one;
    byhhUser * user;
    QRegExp exp1("_([^\"_]+)\"[^:]+: (\\w+) \\(([^\\n]+)\\), [^\\n]+\\n[^:]+: ([^\\n]+)[^(]+\\(([^)]+)");///split the id , author , title , post time
    //QRegExp exp2("\\n\\n((.|\\n)+)\\n--\\n((.|\\n)+)\\n.+武汉白云黄鹤站 bbs.whnet.edu.cn");/// split the content and signature
    //QRegExp blank("(\\n)[^:【]");
    while ( (pos = html.indexOf("<textarea" , pos) ) > -1) {
	int length = html.indexOf("</textarea" , pos) - pos;
	QString temp = html.mid(pos , length);
	pos += length;
	//stage 1 : split the id , author , title , post time
	int arrow = exp1.indexIn(temp);
	if (arrow == -1)    break;
	one = new byhhArticle;
	user = new byhhUser;
	user-> setProperty("id",exp1.cap(2));
	user-> setProperty("nick",exp1.cap(3));
	one -> board = board;
	one -> id = exp1.cap(1);
	one -> author = user;
	one -> title = exp1.cap(4);
	one -> time = exp1.cap(5);
	one -> topic = id;
	one -> setProfile(profile);
	pos += exp1.matchedLength();
	//stage 2: split the content
	arrow = temp.indexOf("\n", exp1.matchedLength()) + 1;
	int arrow2 = temp.indexOf("\n--\n", arrow);
	QString content = temp.mid(arrow, arrow2 - arrow);
	//int cutPoint = 0;
	//while ( (cutPoint = blank.indexIn(content,cutPoint)) > -1)
	//	content = content.remove(cutPoint, 1);
	one -> content = content;
	one -> signature = temp.mid(arrow2 + 3);
	if (! addArticle(one)){		//already exist this article
	    delete one;
	    delete user;
	}
	else
	    emit newStatus(222, tr("Got a new article from topic '%1'").arg(name));
    }
    emit refreshFinished(false);
}
